package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.wbao.dianniu.adapter.FriendSearchAdapter;
import com.wbao.dianniu.data.AccountData;
import com.wbao.dianniu.listener.IAccountSearchListener;
import com.wbao.dianniu.manager.AccountSearchManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

public class FriendSearchActivity extends BaseActivity implements IAccountSearchListener {

    private ImageButton searchBtn;
    private EditText mEditText;
    private ListView mListView;
    private TextView noDataTV;
    private FriendSearchAdapter adapter;
    private List<AccountData> mList;
    private AccountSearchManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_friend_search);
        setTitleName("搜索好友");
        init();
        initManager();
    }

    private void init() {
        searchBtn = (ImageButton) findViewById(R.id.friend_search_button);
        searchBtn.setOnClickListener(this);
        mEditText = (EditText) findViewById(R.id.friend_search_edit);
        noDataTV = (TextView) findViewById(R.id.nodata_text);
        mListView = (ListView) findViewById(R.id.friend_search_listview);
        mList = new ArrayList<>();
        adapter = new FriendSearchAdapter(this);
        adapter.setData(mList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //这里要利用adapter.getItem(position)来获取当前position所对应的对象
                Integer accountId = ((AccountData) adapter.getItem(position)).getId();
                Utils.intoHeadInfoActivity(accountId, FriendSearchActivity.this);
            }
        });
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //1.得到InputMethodManager对象
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    String content = mEditText.getText().toString().trim();
                    if (TextUtils.isEmpty(content)) {
                        Notification.toast(FriendSearchActivity.this, "搜索内容不能为空");
                        return false;
                    }
                    manager.searchAccount(content);
                    return true;
                }
                return false;
            }
        });

    }


    private void initManager() {
        manager = new AccountSearchManager(this);
        manager.addAccountSearchListener(this);
    }

    @Override
    public void onAccountSearchFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccountSearchSuccess(List<AccountData> data) {
        // TODO Auto-generated method stub
        if (null == data || 0 == data.size()) {
            // 未找到匹配数据
            mListView.setVisibility(View.GONE);
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
            mList = data;
            adapter.cleardata();
            adapter.setData(mList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.friend_search_button:
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
                String content = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    Notification.toast(FriendSearchActivity.this, "搜索内容不能为空");
                    return;
                }
                manager.searchAccount(content);
                break;

            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != manager){
            manager.removeAccountSearchListener(this);
        }
    }
}
